<template>
<h1>杨硕员工列表练习</h1>
  <el-form style="width: 600px; margin: 0 auto;">
    <el-form-item label="姓名">
      <el-input v-model="emp.name" placeholder="请输入员工姓名"></el-input>
    </el-form-item>
    <el-form-item label="工资">
      <el-input v-model="emp.salary" placeholder="请输入员工工资"></el-input>
</el-form-item>
    <el-form-item label="岗位">
      <el-input v-model="emp.job" placeholder="请输入员工岗位"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="add" >点我提交</el-button>
    </el-form-item>

    <el-table :data="arr" style="width: 100%">
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="salary" label="工资"></el-table-column>
      <el-table-column prop="job" label="岗位"></el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="danger" @click="del(scope.row,scope.$index)">删除
          </el-button>
        </template>
        </el-table-column>
    </el-table>
  </el-form>

</template>
<script setup>
import {ref} from "vue";
import {ElMessage} from "element-plus";

//定义对象用来双向绑定，收集表单中的数据
const emp=ref({name:'',salary:'',job:''});
//定义数组用来存储数据
const arr = ref([]);
//定义添加员工的方法
const add = () =>{
  if(emp.value.name.trim()==''
      ||emp.value.salary.trim()==''
      ||emp.value.job.trim()==''){
    ElMessage.error("请输入员工信息");
    return;
  }
  arr.value.push(emp.value)//将员工对象添加到数组中
  emp.value={name:'',salary:'',job:''};//清空刚刚收集到的员工数据
  console.log(arr.value);
}
//定义删除员工的方法
const del = (emp,index) =>{
  if(confirm("你忍心删除吗？"+emp.name+'??')){
    arr.value.splice(index,1);
    ElMessage.success("删除成功");
  }
}
</script>


<style scoped>

</style>